<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$user_group = $auth['role'];
$user_groupType = $auth['role_type'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="setFirstApprover"){
        if($_POST['request_id']!=0){
            $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
            $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
            $form_id = $_POST['form_id'];
            $getworkflow_id = $db->query("SELECT workflow_id FROM tbform where id='$form_id'","row");
            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
            $valid = 1;
            $notHead = 0;
            if($_POST['submitType']==1){
                //echo "submit";
                $status = 0;
            }else if($_POST['submitType']==2){
                //echo "draft";
                $status = 4;
            }
            if(!empty($master['child_id'])){
                $d = 0;
                $arrayWorkflowRole = array();
                $arrayWorkflowRoleStatus = array();
                $arrayOrgchart = array();
                $arrayWorkflowObjID = array();
                $push = false;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        //echo $queryMasterNode['role']."\n";
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //for the the submitter
                            if($d==0 and $user_groupType!=1){ //hindi head
                                $dept = $user_group;
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $dept = $queryMasterNodeOrgChart['role'];
                                
                            }
                            //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }else{
                            $dept = "";
                            //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }
                    }
                    //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    array_push($arrayWorkflowRole,$queryMasterNode['role']);
                    array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                    array_push($arrayWorkflowObjID,$master['child_id']);
                    array_push($arrayOrgchart,$dept);
                    $d++;
                }
                $minus = 0;
                //temporary get the flow of the approver
                $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
                for($o=0;$o<count($arrayWorkflowRole);$o++){
                    if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                        if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                            $minus++;
                        }
                        $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        
                        $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        $appFlow = $arrayWorkflowObjID[$o];
                        $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        if($getRowsApprover>0){
                            
                            $lastApproverCount++;
                            
                        }
                    }else{
                        $roleToInserts = $arrayWorkflowRole[$o];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        $appFlow = $arrayWorkflowObjID[$o];
                        $approver = $arrayWorkflowRole[$o];
                        if($getRowsApprover>0){
                            $lastApproverCount++;
                        }
                    }
                    $requestFlowObj_id.=$appFlow."<->";
                    $requestFlowApprover.=$approver."<->";
                }
                //for insert the first approver
                $minus=0;$roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approver = "";
                for($o=0;$o<count($arrayWorkflowRole);$o++){
                    if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                        if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                            $minus++;
                        }
                        $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                            $lastApproverCount++;
                            $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        }
                    }else{
                        $roleToInserts = $arrayWorkflowRole[$o];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                           $approver = $arrayWorkflowRole[$o];
                           $lastApproverCount++;
                        }
                    }
                    if($o==0 && $getRowsApprover==0){
                        $confirm = 1;
                    }
                    if($getRowsApprover>0){
                        $approverStatus = $arrayWorkflowRoleStatus[$o];
                        $master_id = $arrayWorkflowObjID[$o];
                        $proceedToNext++;
                        //echo $approver."\n";
                        break;
                    }
                }
                
            }else{
                //echo "0";
                $valid = 0;
            }
            //update the flow
            $updateRequest = array(
                "requestFlowObj_id"=>$requestFlowObj_id,
                "requestFlowApprover"=>$requestFlowApprover
            );
            $whereUpdateRequest = array(
                "id"=>$_POST['request_id']
            );
            $db->update("tbrequest",$updateRequest,$whereUpdateRequest);
            
            if($_POST['submitType']=="2"){
                $insertRequestProcess = array(
                        "request_id"=>$_POST['request_id'],
                        "approver"=>"",
                        "approverStatus"=>"",
                        "wf_master_id"=>"",
                        "status"=>4
                );
                $db->insert("tbrequest_process",$insertRequestProcess);
                echo "draft^";
            }else{
                if($lastApproverCount==0){
                    $deleteRequest = array(
                        "id"=>$_POST['request_id']
                        );
                    $db->delete("tbrequest",$deleteRequest);
                    echo "No Approver^";
                    $valid = 0;
                }else{
                    if($confirm>0){
                        //echo "confirm muna.\n";
                        //echo $approver."->".$approverStatus."->".$master_id."->"."\n";
                        $arr = array("action"=>"insertConfirmFirstApprover","request_id"=>$_POST['request_id'],"approver"=>$approver,"approverStatus"=>$approverStatus,"wf_master_id"=>$master_id,"confirmation"=>$confirm,"requestFlowObj_id"=>$requestFlowObj_id,"requestFlowApprover"=>$requestFlowApprover);
                        echo json_encode($arr)."^";
                    }else{
                        //diretso insert
                        //echo $approver."->".$approverStatus."->".$master_id."->"."\n";
                        echo "1^";
                        //echo $approverFlow;
                        $insertRequestProcess = array(
                                "request_id"=>$_POST['request_id'],
                                "approver"=>$approver,
                                "approverStatus"=>$approverStatus,
                                "wf_master_id"=>$master_id,
                                "status"=>0
                        );
                        $db->insert("tbrequest_process",$insertRequestProcess);
                    }
                }   
            } 
        }else{
            echo "error";
        }
    }else if($_POST['action']=="updateDraftApprover"){
        $request_id = $_POST['request_id'];
        $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
        $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
        $getworkflow_id = $db->query("SELECT f.workflow_id FROM tbrequest r LEFT JOIN tbform f on f.id=r.form_id WHERE r.id ='$request_id'","row");
        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
        if(!empty($master['child_id'])){
            $d = 0;
            $arrayWorkflowRole = array();
            $arrayWorkflowRoleStatus = array();
            $arrayOrgchart = array();
            $arrayWorkflowObjID = array();
            $push = false;
            while(true){
                if($d==0){
                    $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                }else{
                    $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                }
                $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                if($queryMasterNode['role']=="END"){
                    break;
                }else{
                    //echo $queryMasterNode['role']."\n";
                    if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                        //for the the submitter
                        if($d==0 and $user_groupType!=1){ //hindi head
                            $dept = $user_group;
                            $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                        }else{
                            if($d==0){
                                $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                            }else{
                                $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }
                            $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            $dept = $queryMasterNodeOrgChart['role'];
                            
                        }
                        //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    }else{
                        $dept = "";
                        //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    }
                }
                //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                array_push($arrayWorkflowRole,$queryMasterNode['role']);
                array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                array_push($arrayWorkflowObjID,$master['child_id']);
                array_push($arrayOrgchart,$dept);
                $d++;
            }
            $minus = 0;
            //temporary get the flow of the approver
            $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approverFlow = "";
            for($o=0;$o<count($arrayWorkflowRole);$o++){
                if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                    if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                        $minus++;
                    }
                    $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                    
                    $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                    //echo $roleToInserts."->".$getRowsApprover."\n";
                    $appFlow = $arrayWorkflowObjID[$o];
                    $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                    if($getRowsApprover>0){
                        
                        $lastApproverCount++;
                        
                    }
                }else{
                    $roleToInserts = $arrayWorkflowRole[$o];
                    $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                    //echo $roleToInserts."->".$getRowsApprover."\n";
                    $appFlow = $arrayWorkflowObjID[$o];
                    $approver = $arrayWorkflowRole[$o];
                    if($getRowsApprover>0){
                        $lastApproverCount++;
                    }
                }
                $requestFlowObj_id.=$appFlow."<->";
                $requestFlowApprover.=$approver."<->";
            }
            //for insert the first approver
            $minus=0;$roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;$approver = "";
            for($o=0;$o<count($arrayWorkflowRole);$o++){
                if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                    if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                        $minus++;
                    }
                    $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                    $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                    //echo $roleToInserts."->".$getRowsApprover."\n";
                    if($getRowsApprover>0){
                        $lastApproverCount++;
                        $approver = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                    }
                }else{
                    $roleToInserts = $arrayWorkflowRole[$o];
                    $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                    //echo $roleToInserts."->".$getRowsApprover."\n";
                    if($getRowsApprover>0){
                       $approver = $arrayWorkflowRole[$o];
                       $lastApproverCount++;
                    }
                }
                if($o==0 && $getRowsApprover==0){
                    $confirm = 1;
                }
                if($getRowsApprover>0){
                    $approverStatus = $arrayWorkflowRoleStatus[$o];
                    $master_id = $arrayWorkflowObjID[$o];
                    $proceedToNext++;
                    //echo $approver."\n";
                    break;
                }
            }
            
        }else{
            //echo "0";
            $valid = 0;
        }
        //update the flow
        $updateRequest = array(
            "requestFlowObj_id"=>$requestFlowObj_id,
            "requestFlowApprover"=>$requestFlowApprover
        );
        $whereUpdateRequest = array(
            "id"=>$request_id
        );
        $db->update("tbrequest",$updateRequest,$whereUpdateRequest);
        if($lastApproverCount==0){
            $deleteRequest = array(
                "id"=>$_POST['request_id']
                );
            //$db->delete("tbrequest",$deleteRequest);
            echo "No Approver^";
            $valid = 0;
        }else{
            if($confirm>0){
                //echo "confirm muna.\n";
                //echo $approver."->".$approverStatus."->".$master_id."->"."\n";
                $arr = array("action"=>"updateConfirmFirstApprover","request_id"=>$_POST['request_id'],"approver"=>$approver,"approverStatus"=>$approverStatus,"wf_master_id"=>$master_id,"confirmation"=>$confirm,"requestFlowObj_id"=>$requestFlowObj_id,"requestFlowApprover"=>$requestFlowApprover);
                echo json_encode($arr)."^";
            }else{
                //diretso insert
                //echo $approver." ".$approverStatus." ".$master_id." "."\n";
                echo "1^";
                //echo $approverFlow;
                $updateDraft = array(
                        "approver"=>$approver,
                        "approverStatus"=>$approverStatus,
                        "wf_master_id"=>$master_id,
                        "status"=>0,
                        "approvedBy"=>0
                );
                $whereUpdateRequest = array(
                    "request_id"=>$request_id,
                    "status"=>4
                );
                $db->update("tbrequest_process",$updateDraft,$whereUpdateRequest);
                $updateTBRequest = array(
                    "status"=>0,
                    "updated_by"=>0
                );
                $whereTBRequest = array(
                    "id"=>$request_id
                );
                $db->update("tbrequest",$updateTBRequest,$whereTBRequest);
            }
        }   
    }else if($_POST['action']=="setApprover"){
        $request_id = explode(",",implode(",",$_POST['request_id']));
        $nextMaster_id = explode(",",implode(",",$_POST['nextMaster_id']));
        $requestProcess_id = explode(",",implode(",",$_POST['requestProcess_id']));
        $requestStatus = explode(",",implode(",",$_POST['requestStatus']));
        $sequenceApprover = explode(",",implode(",",$_POST['sequenceApprover']));
        for($a=0;$a<count($request_id);$a++){
            //echo "Request ID: ".$request_id[$a]."\nRequest Process ID: ".$requestProcess_id[$a]."\nNext Approver: ".$nextMaster_id[$a]."\nRequest Status: ".$requestStatus[$a];
            if($nextMaster_id[$a]==1){
                //BACK TO THE APPROVER
                //REQUEST STATUS EITHER DRAFT OR REJECTED ONLY!!!
                //tbrequest
                $update = array(
                    "status"=>$requestStatus[$a],
                    "updated_by"=>$user_id
                    );
                $condition = array(
                    "id"=>$request_id[$a]
                    );
                $db->update("tbrequest",$update,$condition);
                
                //tbrequest_process
                $updateReqProc = array(
                    "status"=>$requestStatus[$a],
                    "approvedBy"=>$user_id,
                    "approver"=>"",
                    "wf_master_id"=>"",
                    "approverStatus"=>""
                    );
                $conditionReqProc = array(
                    "id"=>$requestProcess_id[$a]
                    );
                $db->update("tbrequest_process",$updateReqProc,$conditionReqProc);
                echo "0^";
            }else if($nextMaster_id[$a]==2){
                //UPDATE TO APPROVED NA!!
                $updateReq = array(
                    "status"=>1,
                    "updated_by"=>$user_id
                    );
                $conditionReq = array(
                    "id"=>$request_id[$a]
                    );
                $db->update("tbrequest",$updateReq,$conditionReq);
                //Update status = 2 means last approver
                $update = array(
                    "status"=>2,
                    "approvedBy"=>$user_id
                    );
                $condition = array(
                    "id"=>$requestProcess_id[$a]
                    );
                $db->update("tbrequest_process",$update,$condition);
                echo "done^";
            }else if($nextMaster_id[$a]==3){
                //MOVE TO THE NEXT!
                //echo "".$sequenceApprover[$a]." is the master";
                $updateDone = 0;
                //echo $_POST['requestProcess_id'];
                //next process is to insert the next approver or to update the request to finish
                $getworkflow_id = $db->query("SELECT f.workflow_id FROM tbrequest r LEFT JOIN tbform f on r.form_id =f.id where r.id='$request_id[$a]'","row");
                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$sequenceApprover[$a]."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                $getWorkFlowGroup = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                $approverStatus = $getWorkFlowGroup['status'];
                //echo $getworkflow_id['workflow_id'];
                //echo $getWorkFlowGroup['role']; group type
                $pos = strpos($getWorkFlowGroup['object_id'],"endOfDiagram");
                if($pos==true){
                    //echo "Last Approver";
                    $updateDone +=1;
                }else{
                    if($master['child_id']!=""){
                        $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                        $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $getMasterObjectID = $db->query("SELECT master_id from tborgchartline where child_id='".$getObjectID['object_id']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $getOrgChartGroup = $db->query("SELECT role,object_id from tborgchartobjects where object_id='".$getMasterObjectID['master_id']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                        $lastApproverCount = 0;
                        $d = 0;
                        $arrayWorkflowRole = array();
                        $arrayWorkflowRoleStatus = array();
                        $arrayOrgchart = array();
                        $arrayWorkflowObjID = array();
                        $push = false;
                        while(true){
                            if($d==0){
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '$sequenceApprover[$a]' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }else{
                                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                            }
                            $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                            if($queryMasterNode['role']=="END"){
                                break;
                            }else{
                                //echo $queryMasterNode['role']."\n";
                                if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                    //for the the submitter
                                    if($d==0 and $user_groupType!=1){ //hindi head
                                        $dept = $user_group;
                                        $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    }else{
                                        if($d==0){
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                        }else{
                                            $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                        }
                                        $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                        $dept = $queryMasterNodeOrgChart['role'];
                                        
                                    }
                                    //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                                }else{
                                    $dept = "";
                                    //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                                }
                            }
                            //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                            array_push($arrayWorkflowRole,$queryMasterNode['role']);
                            array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                            array_push($arrayWorkflowObjID,$master['child_id']);
                            array_push($arrayOrgchart,$dept);
                            $d++;
                        }
                        $minus = 0;
                        $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$confirm = 0;$proceedToNext = 0;
                        for($o=0;$o<count($arrayWorkflowRole);$o++){
                            if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                                if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                                    $minus++;
                                }
                                $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                                $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                                //echo $roleToInserts."->".$getRowsApprover."\n";
                                if($getRowsApprover>0){
                                    $lastApproverCount++;
                                    $approver = $roleToInserts;
                                }
                            }else{
                                $roleToInserts = $arrayWorkflowRole[$o];
                                $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                                //echo $roleToInserts."->".$getRowsApprover."\n";
                                if($getRowsApprover>0){
                                   $approver = $roleToInserts;
                                   $lastApproverCount++;
                                }
                            }
                            //echo $roleToInserts."\n";
                            if($o==0 && $getRowsApprover==0){
                                $confirm = 1;
                            }
                            if($getRowsApprover>0){
                                $approverStatus = $arrayWorkflowRoleStatus[$o];
                                $master_id = $arrayWorkflowObjID[$o];
                                $proceedToNext++;
                                //echo $approver."\n";
                                break;
                            }
                        }
                    }else{
                        //echo "Last Approver(Workflow)";
                        $updateDone +=1;
                        //update the request status = 1
                    }    
                }
                
                if($lastApproverCount==0){
                    //echo "Last Approver (No next Approver)";
                    $updateDone +=1;
                }
                if($updateDone>0){
                    if($confirm){
                        $arr = array("action"=>"lastApprover","request_id"=>$request_id[$a],"requestProcess_id"=>$requestProcess_id[$a],"approver"=>"","approverStatus"=>$approverStatus,"wf_master_id"=>"","confirmation"=>$confirm);
                        echo json_encode($arr)."^";
                    }else{
                        //done
                        $updateReq = array(
                            "status"=>1,
                            "updated_by"=>$user_id
                            );
                        $conditionReq = array(
                            "id"=>$request_id[$a]
                            );
                        $db->update("tbrequest",$updateReq,$conditionReq);
                        //Update status = 2 means last approver
                        $update = array(
                            "status"=>2,
                            "approvedBy"=>$user_id
                            );
                        $condition = array(
                            "id"=>$requestProcess_id[$a]
                            );
                        $db->update("tbrequest_process",$update,$condition);
                        echo "done^";
                    }
                }else{
                    //Update status = 1 means approved then insert another approver!
                    if($confirm>0){
                        $arr = array("action"=>"insertApprover","request_id"=>$request_id[$a],"requestProcess_id"=>$requestProcess_id[$a],"approver"=>$approver,"approverStatus"=>$approverStatus,"wf_master_id"=>$master_id,"confirmation"=>$confirm);
                        echo json_encode($arr)."^";
                    }else{
                        echo "1^";
                        $insertRequestProcess = array(
                                "request_id"=>$request_id[$a],
                                "approver"=>$approver,
                                "approverStatus"=>$approverStatus,
                                "wf_master_id"=>$master_id,
                                "status"=>0
                        );
                        $db->insert("tbrequest_process",$insertRequestProcess);
                        $update = array(
                            "status"=>1,
                            "approvedBy"=>$user_id
                            );
                        $condition = array(
                            "id"=>$requestProcess_id[$a]
                            );
                        $db->update("tbrequest_process",$update,$condition);
                    }
                }
            }else{
                //FOR SPECIFIC USER
                $getSubmitter = $db->query("SELECT * FROM tbuser u LEFT JOIN tbrequest r on u.id=r.user_id WHERE r.id='$request_id[$a]'","row");
                $getParent = $db->query("SELECT DISTINCT(wl.master_id) FROM tbworkflow w LEFT JOIN tbworkflow_line wl on w.id = wl.worflow_id LEFT JOIN tbform f on w.id=f.workflow_id LEFT JOIN tbrequest r on f.id=r.form_id where wl.child_id = '".$nextMaster_id[$a]."' and r.id='".$request_id[$a]."'","row");
                $roleSubmitter = $getSubmitter['role'];
                $roleTypeSubmitter = $getSubmitter['role_type'];
                $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$roleSubmitter' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                $getworkflow_id = $db->query("SELECT f.workflow_id FROM tbrequest r LEFT JOIN tbform f on r.form_id =f.id where r.id='$request_id[$a]'","row");
                //echo $getObjectID['object_id'];
                $d = 0;
                $arrayWorkflowRole = array();
                $arrayWorkflowRoleStatus = array();
                $arrayOrgchart = array();
                $arrayWorkflowObjID = array();
                $push = false;
                while(true){
                    if($d==0){
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                    }else{
                        $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                    }
                    $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                    if($queryMasterNode['role']=="END"){
                        break;
                    }else{
                        //echo $queryMasterNode['role']."\n";
                        if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                            //for the the submitter
                            if($d==0 and $roleTypeSubmitter!=1){ //hindi head
                                $dept = $roleSubmitter;
                                $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$roleSubmitter' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                            }else{
                                if($d==0){
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                }else{
                                    $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }
                                $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                $dept = $queryMasterNodeOrgChart['role'];
                            }
                            //echo $queryMasterNode['role']."_".$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }else{
                            $dept = "";
                            //echo $queryMasterNode['role']."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                        }
                    }
                    //echo $queryMasterNode['role'].$dept."->".$master['child_id']."++".$nextMaster_id[$a]."\n\n";
                    array_push($arrayWorkflowRole,$queryMasterNode['role']);
                    array_push($arrayWorkflowRoleStatus,$queryMasterNode['status']);
                    array_push($arrayWorkflowObjID,$master['child_id']);
                    array_push($arrayOrgchart,$dept);
                    $d++;
                }
                $minus = 0;
                $roleToInserts = "";$object_id2 = "";$lastApproverCount=0;$getRowsApprover = 0;$confirmation=0;$compare = 0;$proceedToNext = 0;
                for($o=0;$o<count($arrayWorkflowRole);$o++){
                    if($arrayWorkflowRole[$o]=="Head" || $arrayWorkflowRole[$o]=="Assistant Head"){
                        if($arrayWorkflowRole[$o]=="Head" and $arrayWorkflowRole[$o-1]=="Assistant Head"){
                            $minus++;
                        }
                        $roleToInserts = $arrayWorkflowRole[$o]."_".$arrayOrgchart[$o-$minus];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($arrayWorkflowRole[$o])."' and role='".$arrayOrgchart[$o-$minus]."' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                            $lastApproverCount++;
                            $approver = $roleToInserts;
                        }
                    }else{
                        $roleToInserts = $arrayWorkflowRole[$o];
                        $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where (concat_ws(' ',u.firstname,u.lastname) = '".$roleToInserts."' or p.position = '".$roleToInserts."') and u.is_active=1 and u.status =1 and u.company_id = $company_id","numrows");
                        //echo $roleToInserts."->".$getRowsApprover."\n";
                        if($getRowsApprover>0){
                           $approver = $roleToInserts;
                           $lastApproverCount++;
                        }
                    }
                    if($arrayWorkflowObjID[$o]==$nextMaster_id[$a]){
                        $compare++;
                    }
                    if($compare>0){
                        if($compare==1 and $getRowsApprover==0){
                            $confirmation++;
                        }
                        if($getRowsApprover>0){
                            $approverStatus = $arrayWorkflowRoleStatus[$o];
                            $master_id = $arrayWorkflowObjID[$o];
                            $proceedToNext++;
                            //echo $o.$arrayWorkflowRole[$o]." ".$getRowsApprover."\n";
                            break;
                        }
                    }
                }
                if($getRowsApprover>0){
                //inserted
                    if($confirmation>0){
                        //echo "confirm muna.\n";
                        //echo $approver."->".$approverStatus."->".$master_id."->"."\n";
                        $arr = array("action"=>"insertApprover","request_id"=>$request_id[$a],"requestProcess_id"=>$requestProcess_id[$a],"approver"=>$approver,"approverStatus"=>$approverStatus,"wf_master_id"=>$master_id,"confirmation"=>$confirmation);
                        echo json_encode($arr)."^";
                    }else{
                        //diretso insert
                        //echo $approver."->".$approverStatus."->".$master_id."->"."\n";
                        echo "1^";
                        $insertRequestProcess = array(
                                "request_id"=>$request_id[$a],
                                "approver"=>$approver,
                                "approverStatus"=>$approverStatus,
                                "wf_master_id"=>$master_id,
                                "status"=>0
                        );
                        $db->insert("tbrequest_process",$insertRequestProcess);
                        $update = array(
                            "status"=>1,
                            "approvedBy"=>$user_id
                            );
                        $condition = array(
                            "id"=>$requestProcess_id[$a]
                            );
                        //echo $requestProcess_id[$a];
                        $db->update("tbrequest_process",$update,$condition);
                    }
                }else{
                //done
                    $updateReq = array(
                        "status"=>1,
                        "updated_by"=>$user_id
                        );
                    $conditionReq = array(
                        "id"=>$request_id[$a]
                        );
                    $db->update("tbrequest",$updateReq,$conditionReq);
                    //Update status = 2 means last approver
                    $update = array(
                        "status"=>2,
                        "approvedBy"=>$user_id
                        );
                    $condition = array(
                        "id"=>$requestProcess_id[$a]
                        );
                    $db->update("tbrequest_process",$update,$condition);
                    echo "done^";
                }
            }
        }
    }else if($_POST['action']=="setNextApprover"){
        /*$request_id = explode(",",implode(",",$_POST['request_id']));
        $nextMaster_id = explode(",",implode(",",$_POST['nextMaster_id']));
        $requestProcess_idArray = explode(",",implode(",",$_POST['requestProcess_id']));
        for($a=0;$a<count($request_id);$a++){
            $updateDone = 0;
            //echo $_POST['requestProcess_id'];
            //next process is to insert the next approver or to update the request to finish
            $getworkflow_id = $db->query("SELECT f.workflow_id FROM tbrequest r LEFT JOIN tbform f on r.form_id =f.id where r.id='$request_id[$a]'","row");
            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$nextMaster_id[$a]."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
            $getWorkFlowGroup = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
            $approverStatus = $getWorkFlowGroup['status'];
            //echo $getworkflow_id['workflow_id'];
            //echo $getWorkFlowGroup['role']; group type
            $pos = strpos($getWorkFlowGroup['object_id'],"endOfDiagram");
            if($pos==true){
                //echo "Last Approver";
                $updateDone +=1;
            }else{
                if($master['child_id']!=""){
                    $getActiveOrgChart = $db->query("SELECT id FROM tborgchart where is_active = 1 and company_id='$company_id' and status=1","row");
                    $getObjectID = $db->query("SELECT object_id from tborgchartobjects where role='$user_group' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                    $getMasterObjectID = $db->query("SELECT master_id from tborgchartline where child_id='".$getObjectID['object_id']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                    $getOrgChartGroup = $db->query("SELECT role,object_id from tborgchartobjects where object_id='".$getMasterObjectID['master_id']."' and orgChart_id='".$getActiveOrgChart['id']."'","row");
                    $lastApproverCount = 0;
                    $d = 0;
                    $confirmation = 0;
                    while(true){
                        //FOR WORKFLOW ROLE
                        if($d==0){
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$nextMaster_id[$a]."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                        }else{
                            $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");    
                        }
                        $queryMasterNode = $db->query("SELECT * FROM tbworflow_obects where object_id='".$master['child_id']."' and worflow_id = '".$getworkflow_id['workflow_id']."'","row");
                        //FOR ORGCHART ROLE
                        if($queryMasterNode['role']=="END"){
                            break;
                        }else{
                            if($queryMasterNode['role']=="Head" || $queryMasterNode['role']=="Assistant Head"){
                                //FOR HEAD OR ASSISTANT
                                if($d==0 && $user_groupType==2 && $queryMasterNode['role']=="Head"){
                                    $userRole = $user_group;
                                    $approver =  $queryMasterNode['role']."_".$user_group;
                                    $masterOrgChart = $db->query("SELECT object_id FROM tborgchartobjects where role='$user_group' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                }else{
                                    if($d==0){
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where child_id = '".$getObjectID['object_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                    }else{
                                        $masterOrgChart = $db->query("SELECT master_id FROM tborgchartline where (child_id = '".$masterOrgChart['master_id']."' || child_id='".$masterOrgChart['object_id']."') and orgChart_id = '".$getActiveOrgChart['id']."'","row");    
                                    }
                                    $queryMasterNodeOrgChart = $db->query("SELECT * FROM tborgchartobjects where object_id='".$masterOrgChart['master_id']."' and orgChart_id = '".$getActiveOrgChart['id']."'","row");
                                    $approver =  $queryMasterNode['role']."_".$queryMasterNodeOrgChart['role'];
                                    $userRole = $queryMasterNodeOrgChart['role'];
                                }
                                $getRowsApprover = $db->query("SELECT * FROM tbuser where role_type='".tempGroupType($queryMasterNode['role'])."' and role='$userRole' and is_active=1 and status =1 and company_id = '$company_id'","numrows");
                                if($getRowsApprover>0){
                                    $lastApproverCount++;
                                }
                                
                            }else{
                                //FOR SPECIFIC USER OR POSITION
                                $getRowsApprover = $db->query("SELECT * FROM tbuser u LEFT JOIN tbposition p on u.position_id = p.id where concat_ws(' ',u.firstname,u.lastname) = '".$queryMasterNode['role']."' or p.position = '".$queryMasterNode['role']."' and u.is_active=1 and u.status =1 and u.company_id = '$company_id'","numrows");
                                if($getRowsApprover>0){
                                    $approver = $queryMasterNode['role'];
                                    $lastApproverCount++;
                                }
                            }
                            if($d==0){
                                if($getRowsApprover==0){
                                    $confirmation++;
                                }
                            }
                            if($lastApproverCount>0){
                                $master_id = $queryMasterNode['object_id'];
                                $approverStatus = $queryMasterNode['status'];
                                //echo $approver."->".$master_id."->".$approverStatus;
                                break;
                            }
                        }
                        $d++;
                    }
                }else{
                    //echo "Last Approver(Workflow)";
                    $updateDone +=1;
                    //update the request status = 1
                }    
            }
            if($lastApproverCount==0){
                //echo "Last Approver (No next Approver)";
                $updateDone +=1;
            }
            if($updateDone>0){
                //done
                $updateReq = array(
                    "status"=>1,
                    "updated_by"=>$user_id
                    );
                $conditionReq = array(
                    "id"=>$request_id[$a]
                    );
                $db->update("tbrequest",$updateReq,$conditionReq);
                //Update status = 2 means last approver
                $update = array(
                    "status"=>2,
                    "approvedBy"=>$user_id
                    );
                $condition = array(
                    "id"=>$requestProcess_idArray[$a]
                    );
                $db->update("tbrequest_process",$update,$condition);
                echo "done^";
            }else{
                //Update status = 1 means approved then insert another approver!
                if($confirmation>0){
                    $arr = array("action"=>"insertApprover","request_id"=>$request_id[$a],"requestProcess_id"=>$requestProcess_idArray[$a],"approver"=>$approver,"approverStatus"=>$approverStatus,"wf_master_id"=>$master_id,"confirmation"=>$confirmation);
                    echo json_encode($arr)."^";
                }else{
                    echo "1^";
                    $insertRequestProcess = array(
                            "request_id"=>$request_id[$a],
                            "approver"=>$approver,
                            "approverStatus"=>$approverStatus,
                            "wf_master_id"=>$master_id,
                            "status"=>0
                    );
                    $requestProcess_id = $db->insert("tbrequest_process",$insertRequestProcess);
                    $update = array(
                        "status"=>1,
                        "approvedBy"=>$user_id
                        );
                    $condition = array(
                        "id"=>$requestProcess_idArray[$a]
                        );
                    $db->update("tbrequest_process",$update,$condition);
                }
            }
        }
        */
    }else if($_POST['action']=="insertConfirmFirstApprover"){
        $request_id = $_POST['request_id'];
        $requestProcess_id = $_POST['requestProcess_id'];
        $nextMaster_id = $_POST['nextMaster_id'];
        $approver = $_POST['approver'];
        $approverStatus = $_POST['approverStatus'];
        $requestFlowObj_id = $_POST['requestFlowObj_id'];
        $requestFlowApprover = $_POST['requestFlowApprover'];
        $insertRequestProcess = array(
                "request_id"=>$request_id,
                "approver"=>$approver,
                "approverStatus"=>$approverStatus,
                "wf_master_id"=>$nextMaster_id,
                "status"=>0,
        );
        $db->insert("tbrequest_process",$insertRequestProcess);
        //update the flow
        $updateRequest = array(
            "requestFlowObj_id"=>$requestFlowObj_id,
            "requestFlowApprover"=>$requestFlowApprover
        );
        $whereUpdateRequest = array(
            "id"=>$request_id
        );
        $db->update("tbrequest",$updateRequest,$whereUpdateRequest);
    }else if($_POST['action']=="updateConfirmFirstApprover"){
        $request_id = $_POST['request_id'];
        $nextMaster_id = $_POST['nextMaster_id'];
        $approver = $_POST['approver'];
        $approverStatus = $_POST['approverStatus'];
        $requestFlowObj_id = $_POST['requestFlowObj_id'];
        $requestFlowApprover = $_POST['requestFlowApprover'];
        
        $updateDraft = array(
                "approver"=>$approver,
                "approverStatus"=>$approverStatus,
                "wf_master_id"=>$nextMaster_id,
                "status"=>0,
                "approvedBy"=>0
        );
        $whereUpdateRequest = array(
            "request_id"=>$request_id,
            "status"=>4
        );
        $db->update("tbrequest_process",$updateDraft,$whereUpdateRequest);
        $updateTBRequest = array(
            "status"=>0,
            "updated_by"=>0
        );
        $whereTBRequest = array(
            "id"=>$request_id
        );
        $db->update("tbrequest",$updateTBRequest,$whereTBRequest);
    }else if($_POST['action']=="insertApprover"){
        //echo "Inserted Next Approver\n";
        $request_id = $_POST['request_id'];
        $requestProcess_id = $_POST['requestProcess_id'];
        $nextMaster_id = $_POST['nextMaster_id'];
        $approver = $_POST['approver'];
        $approverStatus = $_POST['approverStatus'];
        for($a=0;$a<count($request_id);$a++){
            //echo $request_id[$a]."\n".$requestProcess_id[$a]."\n".$nextMaster_id."\n".$approver."\n".$approverStatus."\n";
            $insertRequestProcess = array(
                    "request_id"=>$request_id[$a],
                    "approver"=>$approver,
                    "approverStatus"=>$approverStatus,
                    "wf_master_id"=>$nextMaster_id,
                    "status"=>0
            );
            $db->insert("tbrequest_process",$insertRequestProcess);
            $update = array(
                "status"=>1,
                "approvedBy"=>$user_id
                );
            $condition = array(
                "id"=>$requestProcess_id
                );
            $db->update("tbrequest_process",$update,$condition);
            //echo "Inserted";
        }
    }else if($_POST['action']=="rejectRequest"){
        /*$request_id = explode(",",implode(",",$_POST['request_id']));
        $requestProcess_idArray = explode(",",implode(",",$_POST['requestProcess_id']));
        print_r($request_id);
        echo "\n";
        print_r($requestProcess_idArray);
        for($a=0;$a<count($request_id);$a++){
            //request
            $update = array(
                "status"=>2,
                "updated_by"=>$user_id
                );
            $condition = array(
                "id"=>$request_id[$a]
                );
            echo $db->update("tbrequest",$update,$condition);
            //request process
            $updateReqProc = array(
                "status"=>2,
                "approvedBy"=>$user_id
                );
            $conditionReqProc = array(
                "id"=>$requestProcess_idArray[$a]
                );
            $db->update("tbrequest_process",$updateReqProc,$conditionReqProc);
        }
        $request_id = $_POST['request_id'];
        $requestProcess_id = $_POST['requestProcess_id'];
        */
    }else if($_POST['action']=="cancelRequest"){
        //tbrequest
        $update = array(
            "status"=>3
            );
        $condition = array(
            "id"=>$_POST['request_id']
            );
        $db->update("tbrequest",$update,$condition);
        
        $update2 = array(
            "status"=>3,
            "approver"=>0
            );
        $condition2 = array(
            "request_id"=>$_POST['request_id'],
            "status"=>0
            );
        $db->update("tbrequest_process",$update2,$condition2);
        echo 1;
    }else if($_POST['action']=="confDeleteRequest"){
        $deleteRequest = array(
            "id"=>$_POST['request_id']
            );
        $db->delete("tbrequest",$deleteRequest);
        echo "deleted";
    }else if($_POST['action']=="lastApprover"){
        //done
        $request_id = $_POST['request_id'];
        $requestProcess_id = $_POST['requestProcess_id'];
        $updateReq = array(
            "status"=>1,
            "updated_by"=>$user_id
            );
        $conditionReq = array(
            "id"=>$request_id
            );
        $db->update("tbrequest",$updateReq,$conditionReq);
        //Update status = 2 means last approver
        $update = array(
            "status"=>2,
            "approvedBy"=>$user_id
            );
        $condition = array(
            "id"=>$requestProcess_id
            );
        $db->update("tbrequest_process",$update,$condition);
        echo "done^";
    }
}else{
    echo "0";
}
?>